#include <iostream>
#include <sstream>
#include <string>
#include <map>

#include "Dealer.h"
#include "QtApp.h"

using namespace std;

/*
 * Vrai si il doit délivrer les cartes
 * Faux si il ne doit pas délivrer les cartes
 */
Dealer::~Dealer()
{

}
bool Dealer::isDelivering()
{
	return isDistribute;
}
/*
 * Inverse la position de 2 joueurs
 */
void Dealer::reversePlayer(Player &player1, Player &player2)
{

	int pos1 = player1.getPosition();
	int pos2 = player2.getPosition();
	player1.setPosition(pos2);
	player2.setPosition(pos1);
	//TODO
	// changement joueur courant /
	if(Game::getGame()->gameinfo.getCurrentPlayer() == 1)
	 Game::getGame()->gameinfo.setCurrentPlayer(2);
	else
	 Game::getGame()->gameinfo.setCurrentPlayer(1);

}

void Dealer::printCards(map <int,Card> &cards)
{
	map<int, Card>::iterator it;
	cout << "list cartes du paquet" << endl;
	for (it = cards.begin (); it != cards.end (); ++it)
	{
		int key = it->first;
		Card card = it->second;
		cout << "       id carte : "<< key << "-carte : " << card.toString().toStdString() << endl;
	}
}
map<int,Card> Dealer::initCards()
{
	if( compteurDePartie == nbPartieAvantNouvelleDistribution ||  compteurDePartie <= 0)
	{
		Card c1(1,"A coeur","../cartes/Ah.gif",'c','A');
		cards[1] = c1;
		Card c2(2,"2 coeur","../cartes/2h.gif",'c','2');
		cards[2] = c2;
		Card c3(3,"3 coeur","../cartes/3h.gif",'c','3');
		cards[3] = c3;
		Card c4(4,"4 coeur","../cartes/4h.gif",'c','4');
		cards[4] = c4;
		Card c5(5,"5 coeur","../cartes/5h.gif",'c','5');
		cards[5] = c5;
		Card c6(6,"6 coeur","../cartes/6h.gif",'c','6');
		cards[6] = c6;
		Card c7(7,"7 coeur","../cartes/7h.gif",'c','7');
		cards[7] = c7;
		Card c8(8,"8 coeur","../cartes/8h.gif",'c','8');
		cards[8] = c8;
		Card c9(9,"9 coeur","../cartes/9h.gif",'c','9');
		cards[9] = c9;
		Card c10(10,"10 coeur","../cartes/10h.gif",'c','T');
		cards[10] = c10;
		Card c11(11,"J coeur","../cartes/Jh.gif",'c','J');
		cards[11] = c11;
		Card c12(12,"Q coeur","../cartes/Qh.gif",'c','Q');
		cards[12] = c12;
		Card c13(13,"K coeur","../cartes/Kh.gif",'c','K');
		cards[13] = c13;

		Card c14(14,"A pique","../cartes/As.gif",'p','A');
		cards[14] = c14;
		Card c15(15,"2 pique","../cartes/2s.gif",'p','2');
		cards[15] = c15;
		Card c16(16,"3 pique","../cartes/3s.gif",'p','3');
		cards[16] = c16;
		Card c17(17,"4 pique","../cartes/4s.gif",'p','4');
		cards[17] = c17;
		Card c18(18,"5 pique","../cartes/5s.gif",'p','5');
		cards[18] = c18;
		Card c19(19,"6 pique","../cartes/6s.gif",'p','6');
		cards[19] = c19;
		Card c20(20,"7 pique","../cartes/7s.gif",'p','7');
		cards[20] = c20;
		Card c21(21,"8 pique","../cartes/8s.gif",'p','8');
		cards[21] = c21;
		Card c22(22,"9 pique","../cartes/9s.gif",'p','9');
		cards[22] = c22;
		Card c23(23,"10 pique","../cartes/10s.gif",'p','T');
		cards[23] = c23;
		Card c24(24,"J pique","../cartes/Js.gif",'p','J');
		cards[24] = c24;
		Card c25(25,"Q pique","../cartes/Qs.gif",'p','Q');
		cards[25] = c25;
		Card c26(26,"K pique","../cartes/Ks.gif",'p','K');
		cards[26] = c26;

		Card c27(27,"A carreau","../cartes/Ad.gif",'k','A');
		cards[27] = c27;
		Card c28(28,"2 carreau","../cartes/2d.gif",'k','2');
		cards[28] = c28;
		Card c29(29,"3 carreau","../cartes/3d.gif",'k','3');
		cards[29] = c29;
		Card c30(30,"4 carreau","../cartes/4d.gif",'k','4');
		cards[30] = c30;
		Card c31(31,"5 carreau","../cartes/5d.gif",'k','5');
		cards[31] = c31;
		Card c32(32,"6 carreau","../cartes/6d.gif",'k','6');
		cards[32] = c32;
		Card c33(33,"7 carreau","../cartes/7d.gif",'k','7');
		cards[33] = c33;
		Card c34(34,"8 carreau","../cartes/8d.gif",'k','8');
		cards[34] = c34;
		Card c35(35,"9 carreau","../cartes/9d.gif",'k','9');
		cards[35] = c35;
		Card c36(36,"10 carreau","../cartes/10d.gif",'k','T');
		cards[36] = c36;
		Card c37(37,"J carreau","../cartes/Jd.gif",'k','J');
		cards[37] = c37;
		Card c38(38,"Q carreau","../cartes/Qd.gif",'k','Q');
		cards[38] = c38;
		Card c39(39,"K carreau","../cartes/Kd.gif",'k','K');
		cards[39] = c39;

		Card c40(40,"A treffle","../cartes/Ac.gif",'t','A');
		cards[40] = c40;
		Card c41(41,"2 treffle","../cartes/2c.gif",'t','2');
		cards[41] = c41;
		Card c42(42,"3 treffle","../cartes/3c.gif",'t','3');
		cards[42] = c42;
		Card c43(43,"4 treffle","../cartes/4c.gif",'t','4');
		cards[43] = c43;
		Card c44(44,"5 treffle","../cartes/5c.gif",'t','5');
		cards[44] = c44;
		Card c45(45,"6 treffle","../cartes/6c.gif",'t','6');
		cards[45] = c45;
		Card c46(46,"7 treffle","../cartes/7c.gif",'t','7');
		cards[46] = c46;
		Card c47(47,"8 treffle","../cartes/8c.gif",'t','8');
		cards[47] = c47;
		Card c48(48,"9 treffle","../cartes/9c.gif",'t','9');
		cards[48] = c48;
		Card c49(49,"10 treffle","../cartes/10c.gif",'t','T');
		cards[49] = c49;
		Card c50(50,"J treffle","../cartes/Jc.gif",'t','J');
		cards[50] = c50;
		Card c51(51,"Q treffle","../cartes/Qc.gif",'t','Q');
		cards[51] = c51;
		Card c52(52,"K treffle","../cartes/Kc.gif",'t','K');
		cards[52] = c52;

		return cards;
	}
	else
	{
		return cards;
	}
}

void Dealer::distribution4(Player & player1, Player & player2)
{
	// si partie qui nécciste une distribution ou si les cartes n'ont pas encore était distribué
	if( compteurDePartie == nbPartieAvantNouvelleDistribution || Game::getGame()->gameinfo.getBoardCards()[3].getNumero() == -1 )
	{
		// 4ème carte flop /
		int v1 = (rand()%(cards.size()-1))+1;
		int b1=1;
		for( map <int,Card>::iterator ii=cards.begin(); ii!=cards.end(); ++ii)
		{
			if(b1==v1){ Game::getGame()->gameinfo.setBoardCards(3,(Card)(*ii).second); break;} else b1++;
		}
		Game::getGame()->cards.erase(v1);

		//sauvegarde joueurs
		playerMemory[1] = player1;
		playerMemory[2] = player2;
	}
}

void Dealer::distribution5(Player & player1, Player & player2)
{
	// si partie qui nécciste une distribution ou si les cartes n'ont pas encore était distribué
	if( compteurDePartie == nbPartieAvantNouvelleDistribution || Game::getGame()->gameinfo.getBoardCards()[4].getNumero() == -1 )
	{
		// 5ème carte flop /
		int v1 = (rand()% (cards.size()-1) )+1;
		int b1=1;
		for( map <int,Card>::iterator ii=cards.begin(); ii!=cards.end(); ++ii)
		{
			if(b1==v1){ Game::getGame()->gameinfo.setBoardCards(4,(Card)(*ii).second); break;} else b1++;
		}
		Game::getGame()->cards.erase(v1);

		//sauvegarde joueurs
		playerMemory[1] = player1;
		playerMemory[2] = player2;
	}
}

void Dealer::distributionFlop(Player & player1, Player & player2)
{
	// si partie qui nécciste une distribution ou si les cartes n'ont pas encore était distribué
	if( compteurDePartie == nbPartieAvantNouvelleDistribution || Game::getGame()->gameinfo.getBoardCards()[0].getNumero() == -1 )
	{
		// 1ère carte flop /
		int v1 = (rand()% (cards.size()-1) )+1;
		int b1=1;
		for( map <int,Card>::iterator ii=cards.begin(); ii!=cards.end(); ++ii)
		{
			if(b1==v1) { Card c=(Card)(*ii).second; Game::getGame()->gameinfo.setBoardCards(0,c); break;} else b1++;
		}
		Game::getGame()->cards.erase(v1);

		// 2ème carte flop /
		int v2 = (rand()% (cards.size()-1) )+1;
		int b2=1;
		for( map <int,Card>::iterator ii=cards.begin(); ii!=cards.end(); ++ii)
		{
			if(b2==v2) {Game::getGame()->gameinfo.setBoardCards(1,(Card)(*ii).second); break;} else b2++;
		}
		Game::getGame()->cards.erase(v1);

		// 3ème carte flop /
		int v3 = (rand()% (cards.size()-1) )+1;
		int b3=1;
		for( map <int,Card>::iterator ii=cards.begin(); ii!=cards.end(); ++ii)
		{
			if(b3==v3){Game::getGame()->gameinfo.setBoardCards(1,(Card)(*ii).second); break;} else b3++;
		}	

		Game::getGame()->cards.erase(v3);

		//sauvegarde joueurs
		playerMemory[1] = player1;
		playerMemory[2] = player2;
	}
}
/*
void Dealer::distributionPreFlop(Player & player1, Player & player2)
{
	if( compteurDePartie == nbPartieAvantNouvelleDistribution || compteurDePartie <= 0)
	{
		// 1ère carte joueur 1 /
		int v1 = (rand()% (Game::getGame()->cards.size()-1) )+1;
		int b1=1;
		for( map <int,Card>::iterator ii=Game::getGame()->cards.begin(); ii!=Game::getGame()->cards.end(); ++ii)
		{
			if(b1==v1){ player1.hand[0] = (Card)(*ii).second; break;} else b1++;
		}
		Game::getGame()->cards.erase(v1);

		// 2nd carte joueur 1 /
		int v2 = (rand()% (Game::getGame()->cards.size()-1) )+1;

		int b2=1;
		for( map <int,Card>::iterator ii=Game::getGame()->cards.begin(); ii!=Game::getGame()->cards.end(); ++ii)
		{
			if(b2==v2){player1.hand[1] = (Card)(*ii).second; break;} else b2++;
		}
		Game::getGame()->cards.erase(v2);

		// 1ère carte joueur 2/
		int v3 = (rand()% (cards.size()-1) )+1;
		int b3=1;
		for( map <int,Card>::iterator ii=Game::getGame()->cards.begin(); ii!=Game::getGame()->cards.end(); ++ii)
		{
			if(b3==v3){ player2.hand[0] = (Card)(*ii).second; break;} else b3++;
		}
		Game::getGame()->cards.erase(v3);

		// 2nd carte joueur 2 /
		int v4 = (rand()% (cards.size()-1) )+1;
		int b4=1;
		for( map <int,Card>::iterator ii=Game::getGame()->cards.begin(); ii!=Game::getGame()->cards.end(); ++ii)
		{
			if(b4==v4){player2.hand[1] = (Card)(*ii).second; break;} else b4++;
		}
		Game::getGame()->cards.erase(v4);
	}
}
*/
map <int,Player> Dealer::runGame(	string ipjoueur1, string ipjoueur2, string iport1, string iport2, int amountPlayer1,int amountPlayer2, string strategy1, string strategy2)
{
	cout << "----------Start Dealer::runGame (Nouvelle partie)--------------------" << endl;
	cout << "compteurDePartie("<<compteurDePartie<<") == nbPartieAvantNouvelleDistribution("<<nbPartieAvantNouvelleDistribution<<")" << endl;

	if(compteurDePartie == nbPartieAvantNouvelleDistribution || compteurDePartie <= 0 )
	{

		// joueur 1 /player
		Player player1("olivier");
		player1.setIp(ipjoueur1.c_str());
		player1.setPort(iport1.c_str());

		// joueur 2 /
		Player player2("arnaud");
		player2.setIp( ipjoueur2.c_str() );
		player2.setPort( iport2.c_str() );

		//initialisation des sommes
		player1.setBankroll( amountPlayer1 );
		player2.setBankroll( amountPlayer2 );

		// initialisation des positions
		player1.setPosition( 1 );
		player2.setPosition( 2 );

		player1.setStrategy(strategy1.c_str());
		player2.setStrategy(strategy2.c_str());
		cout << "---------------------Affiche toute les cartes du paquet---------------" << endl;
		printCards(cards);

		playerMemory.clear();
		playerMemory[1] = player1;
		playerMemory[2] = player2;

		cout << "-----------------------------------------------------------------" << endl;
		cout << "------------------------Finsih Dealer::runGame-------------------" << endl;

		return playerMemory;
	}
	else
	{
		//inversion joueur mémoire
		reversePlayer(playerMemory[1],playerMemory[2]);

		//initialisation des sommes
		playerMemory[1].setMise( 0 );
		playerMemory[2].setMise ( 0 );

		playerMemory[1].setBet( 0 );
		playerMemory[2].setBet( 0 );

		playerMemory[1].setBankroll ( amountPlayer1 );
		playerMemory[2].setBankroll ( amountPlayer2 );

		cout << "------------------ information joueur 1 -------------------" << endl;
		Game::getGame()->gameinfo.getPlayers()[1].printHandle();

		cout << "------------------ information joueur 2 -------------------" << endl;
		Game::getGame()->gameinfo.getPlayers()[2].printHandle();

		cout << "-----------------------------------------------------------------" << endl;
		cout << "------------------------Finsih Dealer::runGame-------------------" << endl;

		return playerMemory;
	}

}
void Dealer::distriblind(Player &player1, Player &player2)
{
	//distribution mise
	player1.setMise ( PokerTool::stringToInt( player1.getBlind( BIGBLIND ) ) ); //BIGBLIND
	player2.setMise ( PokerTool::stringToInt( player2.getBlind( BIGBLIND ) ) ); //SMALLBLIND

	//mise //	pot=0;en cours
	player1.setBet ( player1.getMise() );
	player2.setBet ( player2.getMise() );
	player1.setMise(0);
	player2.setMise(0);

	//sommeTotal
	player1.setBankroll( player1.getBankrollInt() - player1.getBetInt() );
	player2.setBankroll( player2.getBankrollInt() - player2.getBetInt() );

	player1.setAmountToCall(0);
	player2.setAmountToCall(player1.getBetInt() - player2.getBetInt() );

	Game::getGame()->gameinfo.setPot(player1.getBetInt() + player2.getBetInt() );

}
Dealer::Dealer()
{
	nbPartieAvantNouvelleDistribution = NBGAME;
	compteurDePartie = nbPartieAvantNouvelleDistribution;
	isDistribute=true;
}


